<?php

/**
 * Created by PhpStorm.
 * User: d7
 * Date: 2016/1/27
 * Time: 11:04
 */
class Lecturer extends MY_Api_Controller
{
    protected $_page_size;
    function __construct(){
        parent::__construct();
        $this->_page_size = $this->config->item('page_size','default');
    }


    function gets(){
        $recommend = $this->input->get('recommend');
        $page = $this->input->get('page');
        if(empty($page) and !empty($recommend) and $recommend=='index'){
            $this->config->load('data/recommend');
            $lecturers = $this->config->item('index')['recommend_lecturer'];
            foreach($lecturers as &$v){
                $v['name']=$v['title'];
                $v['avatar']=$v['cover'];
                unset($v['title']);
                unset($v['cover']);
                unset($v);
            }
        }else{

            $this->load->model('lecturer/Lecturer_model');
            $page = $page>0 ? $page : 1;
            $field  = $this->input->get('field');
            $field  = empty($field)?'id':$field;
            $sort   = $this->input->get('sort')=='asc'?'asc':'desc';
            //$status = $this->input->get('status');
            $condition = array(
                'is_del'    =>  0,
            );
            /*if($status!=='' or $status!==null)
                $condition['status'] = $status;*/
            $options = array(
                'limit' =>array(
                    $this->_page_size,
                    ($page-1)*$this->_page_size
                ),
                'order'=>
                    array($field.' '.$sort)
            );
            $lecturers = $this->Lecturer_model->fetch_array($condition,'name,avatar,intro,score',$options);
        }
        if(!$lecturers){
            $this->set_response(60600);
        }
        $this->load->helper('image');
        foreach($lecturers as &$value){
            //$value['lecturer']['avatar'] = get_small_image($value['lecturer']['avatar'],'lecturer');
            //$value['address_info']['cover'] = get_small_image($value['address_info']['cover'],'place');
            $value['avatar'] = get_small_image($value['avatar'],'_middle');
            unset($value);
        }

        $this->set_response(0,$lecturers);
    }

    function get(){
        $id = $this->input->get('id');
        if($id<0){
            $this->set_response(60601);
        }
        $lecturer = $this->Lecturer_model->getOne(array('id'=>$id));
        if(!$lecturer)
            $this->set_response(60600);
        $this->set_response(0,$lecturer);
    }

    function get_comments(){
        $this->set_response(false);
        //todo:  next
    }
}